﻿using System;
using System.Data.SqlClient;
using Eassets.Layered.Domain.Global;
using System.Data;
using System.Collections.ObjectModel;

namespace Eassets.Data.Global
{
    public class AuthorizationGroupData
    {
        /// <summary>
        /// Inserts authorizationGroup.
        /// </summary>
        /// <param name="authorizationGroup"></param>
        public void AddAuthorizationGroup(AuthorizationGroup authorizationGroup)
        {
            SqlCommand command = new SqlCommand("SP_AddAuthorizationGroup"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, authorizationGroup));
        }

        /// <summary>
        /// Updates authorizationGroup.
        /// </summary>
        /// <param name="authorizationGroup"></param>
        public void ModifyAuthorizationGroup(AuthorizationGroup authorizationGroup)
        {
            SqlCommand command = new SqlCommand("SP_ModifyAuthorizationGroup"); // Name of the SP
            command.Parameters.AddWithValue("@AuthorizationGroupId", authorizationGroup.AuthorizationGroupId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, authorizationGroup));
        }


        /// <summary>
        ///  Delete authorizationGroup.
        /// </summary>
        /// <param name="authorizationGroupId"></param>
        public void DeleteAuthorizationGroup(long authorizationGroupId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteAuthorizationGroup"); // Name of the SP
            command.Parameters.AddWithValue("@AuthorizationGroupId", authorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId authorizationGroup.
        /// </summary>
        /// <param name="authorizationGroupId"></param>
        /// <returns></returns>
        public AuthorizationGroup GetByIdAuthorizationGroup(long authorizationGroupId)
        {
            SqlCommand command = new SqlCommand("SP_GetAuthorizationGroupById");
            command.Parameters.AddWithValue("@AuthorizationGroupId", authorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AuthorizationGroup authorizationGroup = new AuthorizationGroup();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupId"].ToString())))
                {
                    authorizationGroup.AuthorizationGroupId = Convert.ToInt64(dataReader["AuthorizationGroupId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    authorizationGroup.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    authorizationGroup.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    authorizationGroup.Description = Convert.ToString(dataReader["Description"]);
                }
            }
            dataReader.Close();
            return authorizationGroup;
        }

        /// <summary>
        /// GetAll authorizationGroup.
        /// </summary>
        /// <returns></returns>
        public Collection<AuthorizationGroup> GetAllAuthorizationGroup()
        {
            Collection<AuthorizationGroup> authorizationGroups = new Collection<AuthorizationGroup>();
            SqlCommand command = new SqlCommand("SP_GetAllAuthorizationGroups");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AuthorizationGroup authorizationGroup;
            while (dataReader.Read())
            {
                authorizationGroup = new AuthorizationGroup();
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupId"].ToString())))
                {
                    authorizationGroup.AuthorizationGroupId = Convert.ToInt64(dataReader["AuthorizationGroupId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    authorizationGroup.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    authorizationGroup.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    authorizationGroup.Description = Convert.ToString(dataReader["Description"]);
                }

                authorizationGroups.Add(authorizationGroup);
            }
            dataReader.Close();
            return authorizationGroups;
        }

        public SqlCommand ConstructCommand(SqlCommand command, AuthorizationGroup authorizationGroup)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@OrganizationLevelListId", authorizationGroup.OrganizationLevelListId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Code", authorizationGroup.Code);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", authorizationGroup.Description);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}


